﻿using System;
using System.Xml.Serialization;

using Rmlife.Data;

namespace Rmlife.Site.ScheduledEvents
{
    public class Event
    {
        public Event()
        {
        }

        private IEvent _ievent = null;

        public IEvent IEventInstance
        {
            get
            {
                LoadIEvent();
                return _ievent;
            }
        }

        /// <summary>
        /// 读取任务
        /// </summary>
        private void LoadIEvent()
        {
            if (_ievent == null)
            {
                if (this.ScheduleType == null)
                {
                    EventLogs.WriteFailedLog("计划任务没有定义其 type 属性");
                }

                Type type = Type.GetType(this.ScheduleType);
                if (type == null)
                {
                    EventLogs.WriteFailedLog(string.Format("计划任务 {0} 无法被正确识别", this.ScheduleType));
                }
                else
                {
                    _ievent = (IEvent)Activator.CreateInstance(type);
                    if (_ievent == null)
                    {
                        EventLogs.WriteFailedLog(string.Format("计划任务 {0} 未能正确加载", this.ScheduleType));
                    }
                }
            }
        }

        private string _key;

        /// <summary>
        /// KEY
        /// </summary>
        public string Key
        {
            get { return this._key; }
            set { this._key = value; }
        }

        private int _timeOfDay = -1;

        /// <summary>
        /// 所处当天时间
        /// </summary>
        public int TimeOfDay
        {
            get { return this._timeOfDay; }
            set { this._timeOfDay = value; }
        }

        private int _minutes = 60;

        /// <summary>
        /// 分钟
        /// </summary>
        public int Minutes
        {
            get
            {
                if (this._minutes < EventManager.TimerMinutesInterval)
                {
                    return EventManager.TimerMinutesInterval;
                }
                return this._minutes;
            }
            set { this._minutes = value; }
        }

        private string _scheduleType;

        /// <summary>
        /// 类型
        /// </summary>
        [XmlAttribute("type")]
        public string ScheduleType
        {
            get { return this._scheduleType; }
            set { this._scheduleType = value; }
        }

        private DateTime _lastCompleted;

        /// <summary>
        /// 任务最后完成时间
        /// </summary>
        [XmlIgnoreAttribute]
        public DateTime LastCompleted
        {
            get { return this._lastCompleted; }
            set
            {
                dateWasSet = true;
                this._lastCompleted = value;
            }
        }

        bool dateWasSet = false;

        /// <summary>
        /// 保证执行
        /// </summary>
        [XmlIgnoreAttribute]
        public bool ShouldExecute
        {
            get
            {
                if (!dateWasSet) 
                {
                 //   LastCompleted = DatabaseProvider.GetInstance().GetLastExecuteScheduledEventDateTime(this.Key, Environment.MachineName);
                }

                if (this.TimeOfDay > -1)
                {
                    DateTime dtNow = DateTime.Now;  //now
                    DateTime dtCompare = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day);
                    return LastCompleted < dtCompare.AddMinutes(this.TimeOfDay) && dtCompare.AddMinutes(this.TimeOfDay) <= DateTime.Now;
                }
                else
                {
                    return LastCompleted.AddMinutes(this.Minutes) < DateTime.Now;
                }
            }
        }

        /// <summary>
        /// 更新时间
        /// </summary>
        public void UpdateTime()
        {
            this.LastCompleted = DateTime.Now;
            //DatabaseProvider.GetInstance().SetLastExecuteScheduledEventDateTime(this.Key, Environment.MachineName, this.LastCompleted);
        }
    }
}
